#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;
    unordered_map<char, char> match = {
        {')', '('},
        {']', '['},
        {'}', '{'},
        {'>', '<'}
    };
    while(n--){
        bool isYes = 1;
        string brackets;
        stack<char> ch;
        cin >> brackets;
        int l = brackets.size();
        for(int i = 0; i < l; ++i){
            char brac = brackets[i];
            if(brac == '<' || brac == '[' || brac == '(' || brac == '{' ){
                ch.push(brac);
            }else{
                if(ch.empty()){
                    isYes = 0;
                    break;
                }else if(ch.top() == match[brac]){
                    ch.pop();
                }else{
                    isYes = 0;
                    break;
                }
            }
        }
        if(!ch.empty()) isYes = 0;
        cout << (isYes ? "Yes" : "No") << endl;
    }
    system("pause");
}